package com.honvay.cola.cloud.framework.security.userdetail;

import com.honvay.cola.cloud.framework.core.constant.CommonConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author LIQIU
 * @date 2018-3-8
 **/
@ApiModel(value="User",description="用户对象user")
public class User implements UserDetails, CredentialsContainer {

    private Long id;

    @ApiModelProperty(value="登录名",name="username")
    private String username;

    private String password;

    @ApiModelProperty(value="状态: 1-可用，0-禁用，-1-锁定",name="status")
    private String status;

    @ApiModelProperty(value="电话号码",name="电话号码")
    private String phoneNumber;

    @ApiModelProperty(value="电子邮箱",name="email")
    private String email;

    @ApiModelProperty(value="用户名称",name="name")
    private String name;

    @ApiModelProperty(value="头像",name="avatar")
    private String avatar;

    @ApiModelProperty(value="用户类型",name="type")
    private String type;

    @ApiModelProperty(value="资源",name="resources")
    private Collection<String> resources = new ArrayList<>();

    @ApiModelProperty(value="角色",name="roles")
    private Collection<String> roles = new ArrayList<>();

    private Collection<GrantedAuthority> grantedAuthorities;

    private Long tenantId;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (grantedAuthorities == null) {
            this.grantedAuthorities = this.getRoles().stream().map(role -> new SimpleGrantedAuthority(role)).collect(Collectors.toList());
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return CommonConstant.USER_STATUS_ENABLED.equals(status);
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return CommonConstant.USER_STATUS_ENABLED.equals(status);
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTenantId() {
        return tenantId;
    }

    public void setTenantId(Long tenantId) {
        this.tenantId = tenantId;
    }

    public Collection<String> getRoles() {
        return roles;
    }

    public void setRoles(Collection<String> roles) {
        this.roles = roles;
    }

    public Collection<String> getResources() {
        return resources;
    }

    public void setResources(Collection<String> resources) {
        this.resources = resources;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }


    public void setName(String name) {
        this.name = name;

    }

    @Override
    public void eraseCredentials() {
        this.password = null;
    }



}
